<?php

//新闻详细页面控制器
class NewsDetailsAction extends CommonAction {

    public function index() {
        C('TOKEN_ON',false);
        if(isset($_GET['id'])) {
            $mem = parent::useMemcached();
            $id = $this->_get('id');
            $newsNav = D('PressNav');
            $news = D('News');
            //增加阅读次数
            $news->where('id='.$id)->setInc('view_count', 1);

            if(!$mem->get("newdetails-$id")) {
                $current = $news->field(array('id', 'nav', 'title', 'content', 'summary', 'view_count', 'addtime', 'from', 'writer', 'is_commend'))
                                ->where('id=' . $id)
                                ->find();
                $mem->set("newdetails-$id", $current, MEMCACHE_COMPRESSED, 0);
            } else {
                $current = $mem->get("newdetails-$id");
            }

            if(!$mem->get("currentNav-$id")) {
                $currentNav = $newsNav->field(array('id', 'name'))
                                        ->where('id=' . $current['nav'])
                                        ->find();
                $mem->set("currentNav-$id", $currentNav, MEMCACHE_COMPRESSED, 0);
            } else {
                $currentNav = $mem->get("currentNav-$id");
            }

            if(!$mem->get("simArticle-$id")) {
                $simArticle = $news->field(array('id', 'title', 'title' => 'ftitle', 'addtime'))
                                    ->limit(8)
                                    ->order('addtime DESC')
                                    ->where('nav=' . $current['nav'])
                                    ->select();
                $mem->set("simArticle-$id", $simArticle, MEMCACHE_COMPRESSED, 60);
            } else {
                $simArticle = $mem->get("simArticle-$id");
            }

            //查询评论
            $newsCommend = M('NewsCommend');
            if(!$mem->get("fourCommend-$id")) {
                $fourCommend = $newsCommend->field(array('content', 'user', 'addtime', 'floor'))
                                            ->where('nid=' . $id)
                                            ->limit(4)
                                            ->order('addtime DESC')
                                            ->select();
                $mem->set("fourCommend-$id", $fourCommend, MEMCACHE_COMPRESSED, 30);
            } else {
                $fourCommend = $mem->get("fourCommend-$id");
            }

            //获取文章评论总数
            $count = $newsCommend->where('nid='.$id)->count();
            //同类热门
            if(!$mem->get("hot-$id")) {
                $hot = $news->field(array('id', 'title', 'addtime', 'title' => 'ftitle'))
                            ->where('nav=' . $current['nav'])
                            ->limit(8)
                            ->select();
                $mem->set("hot-$id", $hot, MEMCACHE_COMPRESSED, 30);
            } else {
                $hot = $mem->get("hot-$id");
            }

            $this->assign(array(
               'current'=>$current,
               'currentnav'=>$currentNav,
               'simarticle'=>$simArticle,
               'fourcommend'=>$fourCommend,
               'count'=>$count,
               'hot'=>$hot,
               'empty'=>'<p style="text-align:center;height:20px;line-height:20px;border-bottom:1px solid #ccc;color:red;">暂时没人评论</p>'
            ));
            $this->display();
        }
    }

}